package database.dao;

import database.util.DaoUtil;
import database.util.SQLSurvey;
import patient.Patient;
import patient.Survey;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class SurveyDao {
    private DaoFactory daoFactory;

    public SurveyDao(DaoFactory daoFactory) {
        this.daoFactory = daoFactory;
    }

    public List<Survey> getAllSurveysByPatient(Patient patient) {
        List<Survey> surveys = new ArrayList<Survey>();

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = daoFactory.getConnection();
            preparedStatement = connection.prepareStatement(SQLSurvey.selectAllQueeryByPatientId(patient.getId()));
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                surveys.add(getSurveyByResultSet(resultSet));
            }
            System.out.println("Karta pacjenta pobrana z bazy dla pacjenta o id = " + patient.getId());
        } catch (SQLException e) {
            System.out.println(e.getMessage() + Arrays.toString(e.getStackTrace()));
        } finally {
            DaoUtil.close(connection, preparedStatement, resultSet);
        }

        return surveys;
    }

    public void saveSurvey(Survey survey, Patient patient) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet generatedKeys = null;
        try {
            connection = daoFactory.getConnection();
            preparedStatement = DaoUtil.prepareStatement(connection, SQLSurvey.getSaveSurveyQuery(survey, patient.getId()), false);
            preparedStatement.executeUpdate();
        } catch (SQLException e) {
            System.out.println(e.getMessage() + Arrays.toString(e.getStackTrace()));
        } finally {
            DaoUtil.close(connection, preparedStatement);
        }
    }

    private Survey getSurveyByResultSet(ResultSet resultSet) throws SQLException {
        Date date = resultSet.getDate(SQLSurvey.W_DATE);
        Double temperature = resultSet.getDouble(SQLSurvey.W_TEMPERATURE);
        return new Survey(date, temperature);
    }

    public boolean isThereThatDate(java.util.Date date, Patient patient) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet generatedKeys = null;
        try {
            connection = daoFactory.getConnection();
            preparedStatement = DaoUtil.prepareStatement(connection, SQLSurvey.getSelectSurveryByDate(date, patient.getId()), false);
            preparedStatement.executeQuery();
            generatedKeys = preparedStatement.getResultSet();
            if (generatedKeys.next()) return true;
            else return false;
        } catch (SQLException e) {
            System.out.println(e.getMessage() + Arrays.toString(e.getStackTrace()));
        } finally {
            DaoUtil.close(connection, preparedStatement);
        }
        return false;
    }

    public List<Survey> getSurveysFromLastMonth(Patient patient) {
        List<Survey> surveys = new ArrayList<Survey>();
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            connection = daoFactory.getConnection();
            preparedStatement = DaoUtil.prepareStatement(connection, SQLSurvey.getSelectSurveryFromLastMonth(patient.getId()), false);
            resultSet = preparedStatement.executeQuery();

            while (resultSet.next()) {
                surveys.add(getSurveyByResultSet(resultSet));
            }
            System.out.println("Karta pacjenta pobrana z bazy na ostatni miesiąc dla pacjenta o id = " + patient.getId());
            System.out.println(surveys);
        } catch (SQLException e) {
            System.out.println(e.getMessage() + Arrays.toString(e.getStackTrace()));
        } finally {
            DaoUtil.close(connection, preparedStatement);
        }
        return surveys;
    }

}
